﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using suduku;

namespace Sudukux
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void operateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox[] box=new TextBox[16];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            //2nd row
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            //3rd row
            box[8] = textBox9;
            box[9] = textBox10;
            box[10] = textBox11;
            box[11] = textBox12;
            //4th row
            box[12] = textBox13;
            box[13] = textBox14;
            box[14] = textBox15;
            box[15] = textBox16;
            ArrayList list1 = new ArrayList();
            ArrayList list2 = new ArrayList();
            ArrayList list3 = new ArrayList();
            int k=0,i,j;
            int sign = 0;
            for (i = 0; i < 16; i++) 
            {
                if (box[i].Text != "") 
                {
                    sign = 1;
                    k++;
                    list1.Add(i/4+1);
                    list2.Add(i%4+1);
                    list3.Add(Convert.ToInt32(box[i].Text));
                }
            }
            if (sign != 0)
            {
                int[,] dd = new int[k, 3];
                j = 0;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list1[i];
                }
                j = 1;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list2[i];
                }
                j = 2;
                for (i = 0; i < k; i++)
                {
                    dd[i, j] = (int)list3[i];
                }
                int mm = 2;
                suduku.sudoku su = new suduku.sudoku();
                //   MatrixS.MatrixS su1 = new MatrixS.MatrixS();
                MathWorks.MATLAB.NET.Arrays.MWNumericArray d = new MathWorks.MATLAB.NET.Arrays.MWNumericArray(dd);
                MathWorks.MATLAB.NET.Arrays.MWNumericArray m = (MathWorks.MATLAB.NET.Arrays.MWNumericArray)mm;
                MWArray S = su.sudokux(m, d);
                double[,] SInt = (double[,])S.ToArray();
                /*
                box[0].Text = ((int)SInt[0, 0]).ToString();
                box[1].Text = ((int)SInt[0, 1]).ToString();
                box[2].Text = ((int)SInt[0, 2]).ToString();
                box[3].Text = ((int)SInt[0, 3]).ToString();

                box[4].Text = ((int)SInt[1, 0]).ToString();
                box[5].Text = ((int)SInt[1, 1]).ToString();
                box[6].Text = ((int)SInt[1, 2]).ToString();
                box[7].Text = ((int)SInt[1, 3]).ToString();

                box[8].Text = ((int)SInt[2, 0]).ToString();
                box[9].Text = ((int)SInt[2, 1]).ToString();
                box[10].Text = ((int)SInt[2, 2]).ToString();
                box[11].Text = ((int)SInt[2, 3]).ToString();

                box[12].Text = ((int)SInt[3, 0]).ToString();
                box[13].Text = ((int)SInt[3, 1]).ToString();
                box[14].Text = ((int)SInt[3, 2]).ToString();
                box[15].Text = ((int)SInt[3, 3]).ToString();
                */
                k = 0;
                for (i = 0; i < 4; i++)
                {
                    for (j = 0; j < 4; j++)
                    {
                        box[k++].Text = ((int)SInt[i, j]).ToString();
                    }
                }
                for (i = 0; i < 16; i++)
                {
                    box[i].ReadOnly = true;
                }
            }
            else
            {
                Warning w1 = new Warning();
                w1.ShowDialog();
            }
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[16];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            //2nd row
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            //3rd row
            box[8] = textBox9;
            box[9] = textBox10;
            box[10] = textBox11;
            box[11] = textBox12;
            //4th row
            box[12] = textBox13;
            box[13] = textBox14;
            box[14] = textBox15;
            box[15] = textBox16;
            int i;
            for (i = 0; i < 16; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 16; i++)
            {
                box[i].ReadOnly = false;
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void sample1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[16];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            //2nd row
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            //3rd row
            box[8] = textBox9;
            box[9] = textBox10;
            box[10] = textBox11;
            box[11] = textBox12;
            //4th row
            box[12] = textBox13;
            box[13] = textBox14;
            box[14] = textBox15;
            box[15] = textBox16;

            int i;
            for (i = 0; i < 16; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 16; i++)
            {
                box[i].ReadOnly = false;
            }

            box[0].Text = "1";
            box[1].Text = "2";
            box[2].Text = "3";
        }

        private void sudokuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help h1 = new Help();
            h1.ShowDialog();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About a1 = new About();
            a1.ShowDialog();
        }

        private void sample1ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TextBox[] box = new TextBox[16];
            //bound the textBox from left to right
            //1st row
            box[0] = textBox1;
            box[1] = textBox2;
            box[2] = textBox3;
            box[3] = textBox4;
            //2nd row
            box[4] = textBox5;
            box[5] = textBox6;
            box[6] = textBox7;
            box[7] = textBox8;
            //3rd row
            box[8] = textBox9;
            box[9] = textBox10;
            box[10] = textBox11;
            box[11] = textBox12;
            //4th row
            box[12] = textBox13;
            box[13] = textBox14;
            box[14] = textBox15;
            box[15] = textBox16;

            int i;
            for (i = 0; i < 16; i++)
            {
                box[i].Text = "";
            }
            for (i = 0; i < 16; i++)
            {
                box[i].ReadOnly = false;
            }

            box[0].Text = "1";
            box[4].Text = "2";
            box[8].Text = "3";
        }
    }
}
